package com.f1ashback.bus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.f1ashback.bus.Vo.NoticeVo;
import com.f1ashback.bus.common.AjaxResult;
import com.f1ashback.bus.entity.Notice;
import com.f1ashback.bus.mapper.NoticeMapper;
import com.f1ashback.bus.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public List<NoticeVo> applist() {
        List<NoticeVo> resl = new ArrayList<>();
        List<Notice> notices = noticeMapper.selectList(null);
        for (Notice notice : notices) {
            NoticeVo noticeVo = new NoticeVo();
            BeanUtils.copyProperties(notice, noticeVo);
            if (notice.getStatus() == 1) {
                noticeVo.setStatus("有效");
            } else {
                noticeVo.setStatus("已过期");
            }
            resl.add(noticeVo);
        }
        return resl;
    }

    @Override
    public AjaxResult listNotice() {
        Map<String, List<NoticeVo>> res = new HashMap<>();
        List<NoticeVo> resl = new ArrayList<>();
        List<Notice> notices = noticeMapper.selectList(null);
        for (Notice notice : notices) {
            NoticeVo noticeVo = new NoticeVo();
            BeanUtils.copyProperties(notice, noticeVo);
            if (notice.getStatus() == 1) {
                noticeVo.setStatus("有效");
            } else {
                noticeVo.setStatus("已过期");
            }
            resl.add(noticeVo);
        }
        res.put("res", resl);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult deleteNotice(Long noticeId) {
        int i = 0;
        try {
            i = noticeMapper.deleteById(noticeId);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn(i+"");
            return AjaxResult.error("删除失败！");
        }
        if (i == 0 || i <= 0){
            log.warn(i+"");
            return AjaxResult.error("删除失败！");
        }
        log.warn(i+"");
        return AjaxResult.success("删除成功！");
    }

    @Override
    public AjaxResult updateNotice(NoticeVo noticeVo) {
        int i = 0;
        try {
            Notice notice = new Notice();
            BeanUtils.copyProperties(noticeVo,notice);
            if (noticeVo.getStatus().equals("0") || noticeVo.getStatus().equals("已过期")){
                notice.setStatus(0);
            }else if(noticeVo.getStatus().equals("1") || noticeVo.getStatus().equals("有效")){
                notice.setStatus(1);
            }
            i = noticeMapper.updateById(notice);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败！");
        }
        if (i == 0 || i<= 0){
            return AjaxResult.error("修改失败！");
        }
        return AjaxResult.success("修改成功");
    }

    @Override
    public AjaxResult pubNotice(String noticeD) {
        Notice notice = new Notice();
        int ii = 0;
        try {
            notice.setNoticeDe(noticeD);
            notice.setStatus(1);
            int i = 0;
            for (byte aByte : noticeD.getBytes()) {
                i += aByte;
            }
            notice.setNoticeId(Long.valueOf(String.valueOf(System.currentTimeMillis()).substring(7)) + Long.valueOf(i));
            ii = noticeMapper.insert(notice);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return AjaxResult.error("发布失败！");
        }
        if (ii <= 0 || ii == 0){
            return AjaxResult.error("发布失败！");
        }
        return AjaxResult.success("发布成功！");
    }
}
